<%@ page import="org.springframework.web.context.request.SessionScope" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%request.setAttribute("ctx", request.getContextPath());%>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui-v2.9.14/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/jquery-2.1.0.min.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui_ext/xmSelect/xm-select.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/crypto-js.js"></script>
    <title>添加人员</title>
</head>
<body>
<form class="layui-form layui-row " action="">
    <div class="layui-col-md6">
        <label class="layui-form-label">人员姓名:</label>
        <div class="layui-input-block">
            <input type="text" name="username" id="username" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-col-md6">
        <label class="layui-form-label">登录账号:</label>
        <div class="layui-input-block">
            <input type="text" name="loginName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-col-md6">
        <label class="layui-form-label">手机号:</label>
        <div class="layui-input-block">
            <input type="text" name="phone" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-col-md6">
        <label class="layui-form-label">登录密码:</label>
        <div class="layui-input-block">
            <input type="text" name="password" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-col-md6">
        <label class="layui-form-label">电子邮箱:</label>
        <div class="layui-input-block">
            <input type="text" name="email" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-col-md6">
        <label class="layui-form-label">所属角色:</label>
        <div class="layui-input-block">
        <select name="authId" id="authId">
            <option value="">请选择</option>
        </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!-- 请勿在项目正式环境中引用该 layui.js 地址 -->

<script>
    layui.use(['form', 'laydate', 'util'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var util = layui.util;


        $.post(
            "/user/listRole.do",
            function (res) {
                let data=res.data;
                for (let i = 0; i < data.length; i++) {
                    $("#authId").append("<option value='"+data[i].id+"'>"+data[i].roleName+"</option>")
                }
                form.render('select');
            }
        );

        $("#username").on('blur',function () {
            let username=$("#username").val();
            $.post(
                "/user/checkName.do",
                {username:username},
                function (res) {
                    if(res.code!=0){
                        layer.msg('人员姓名已存在',{time:2000})
                    $("#username").val("");
                    }
                }
            );
        })

        // 提交事件
        form.on('submit(demo1)', function(data){
            var field = data.field; // 获取表单字段值
            // 显示填写结果，仅作演示用

            $.post(
                "/user/add.do",
                field,
                function (res) {
                    if(res.code==0){
                        layer.msg(res.msg,{time:1000},function () {
                            location.href="/user/tolist.do"
                        })
                    }else{
                        layer.msg(res.msg,{time:1000})
                    }
                }
            );
            // 此处可执行 Ajax 等操作
            // …
            return false; // 阻止默认 form 跳转
        });

        // 日期
        laydate.render({
            elem: '#date'
        });

        // 普通事件
        util.on('lay-on', {
            // 获取验证码
            "get-vercode": function(othis){
                var isvalid = form.validate('.demo-phone'); // 主动触发验证，v2.7.0 新增
                // 验证通过
                if(isvalid){
                    layer.msg('手机号规则验证通过');
                    // 此处可继续书写「发送验证码」等后续逻辑
                    // …
                }
            }
        });
    });
</script>

</body>
</html>